package com.example.bishe.util;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.MDC;

import javax.servlet.http.HttpServletResponse;

/**
 * 返回实体类
 *
 * @author: LDH
 * @date: 2022年12月25日 20:00
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "响应信息主体")
public class RestResult<T> {

    @ApiModelProperty(value = "请求的数据")
    private T data;

    @ApiModelProperty(value = "请求码")
    private int code;

    @ApiModelProperty(value = "返回的信息")
    private String message;

    @ApiModelProperty(value = "返回的时间戳")
    private String timestamp;

    @ApiModelProperty(value = "本次请求的日志id")
    private String logId;

    public static <T> RestResult<T> get_success(T data){
        RestResult<T> result = new RestResult<T>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage("获取成功！");
        result.setData(data);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setLogId(MDC.get("requestId"));
        return result;
    }

    public static <T> RestResult<T> set_success(String msg){
        RestResult<T> result = new RestResult<T>();
        result.setCode(HttpServletResponse.SC_OK);
        result.setMessage(msg);
        result.setData(null);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setLogId(MDC.get("requestId"));
        return result;
    }

    public static <T> RestResult<T> error_msg(T data,String msg,int code){
        RestResult<T> result = new RestResult<T>();
        result.setCode(code);
        result.setMessage(msg);
        result.setData(data);
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        result.setLogId(MDC.get("requestId"));
        return result;
    }

}